package com.goldsign.pay.provider.userbase.service.impl;

import com.goldsign.pay.provider.userbase.service.UserBaseApi;
import com.goldsign.pay.provider.userbase.service.UserBaseService;
import com.goldsign.pay.provider.userbase.to.*;
import com.goldsign.pay.provider.userbase.vo.UserInfoDO;
import com.goldsign.pay.common.exception.GcServiceException;
import com.goldsign.pay.common.util.Constant;
import com.goldsign.pay.provider.userbase.service.CommonService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by xiaojingjing on 2018/5/9.
 */
@Service
public class UserBaseApiImpl implements UserBaseApi {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UserBaseService service;

    @Autowired
    CommonService commonService;

    @Override
    public GetUserInfoRespDTO getUserInfoByMobNo(GetUserInfoReqDTO req) {
        logger.info("收到  查询用户信息(getUserInfoByMobNo) 请求:" + req);

        GetUserInfoRespDTO resp = new GetUserInfoRespDTO();

        try {
            if(StringUtils.isEmpty(req.getMob_no())){
                resp.setResp_code(Constant.PARAMETER_ERROR);
                resp.setResp_msg("PARAMETER_ERROR");
                logger.info("参数不能为空：mob_no=" + req.getMob_no());
                return resp;
            }
            UserInfoDO userInfo = service.getUserInfoByMobNo(req.getMob_no());

            resp.setResp_code(Constant.SUCCESS);
            resp.setResp_msg("SUCCESS");
            resp.setUserInfo(userInfo);
        } catch (GcServiceException e) {
            resp.setResp_code(e.getErrorCode());
            resp.setResp_msg(e.getMessage());
        } catch (Exception e) {
            logger.error(req + "_" + e.getMessage());

            resp.setResp_code(Constant.USERBASE_SYSTERM_ERROR);
            resp.setResp_msg("USERBASE_SYSTERM_ERROR");
        }

        logger.info("返回  查询用户信息(getUserInfoByMobNo) 响应:" + resp);
        return resp;
    }

    @Override
    public CreateUserRespDTO createUser(CreateUserReqDTO req) {

        logger.info("收到  创建用户服务(createUser) 请求:" + req);

        CreateUserRespDTO resp = new CreateUserRespDTO();

        try {
            if(StringUtils.isEmpty(req.getMob_no())){
                resp.setResp_code(Constant.PARAMETER_ERROR);
                resp.setResp_msg("PARAMETER_ERROR");
                logger.info("参数不能为空：mob_no=" + req.getMob_no());
                return resp;
            }
            //get user_no
            String userNo = commonService.execGetWaterNo("t_user_info", "user_no");

            service.createUser(req, userNo);

            resp.setResp_code(Constant.SUCCESS);
            resp.setResp_msg("SUCCESS");
            resp.setUser_no(userNo);
        } catch (GcServiceException e) {
            logger.error(req + "_"+ e.getMessage());
            resp.setResp_code(e.getErrorCode());
            resp.setResp_msg(e.getMessage());
        } catch (Exception e) {
            logger.error(req + "_"+ e.getMessage());

            resp.setResp_code(Constant.USERBASE_SYSTERM_ERROR);
            resp.setResp_msg("USERBASE_SYSTERM_ERROR");
        }

        logger.info("返回 创建用户服务(createUser) 响应:" + resp);
        return resp;
    }


    @Override
    public CheckUserLoginPasswdRespDTO checkUserLoginPasswd(CheckUserLoginPasswdReqDTO req) {

        logger.info("收到  检验用户登录密码(checkUserLoginPasswd) 请求:" + req);

        CheckUserLoginPasswdRespDTO resp = new CheckUserLoginPasswdRespDTO();

        try {
            UserInfoDO userInfo = service.getUserInfoByLoginId(req.getLogin_id());
            if (userInfo == null || req.getLogin_passwd() == null) {
                resp.setResp_code(Constant.UNCORRECT_USERNAME_OR_PASSWORD);
                resp.setResp_msg("UNCORRECT_USERNAME_OR_PASSWORD");
                return resp;
            }

            if (!req.getLogin_passwd().equals(userInfo.getLogin_passwd())) {
                resp.setResp_code(Constant.UNCORRECT_USERNAME_OR_PASSWORD);
                resp.setResp_msg("UNCORRECT_USERNAME_OR_PASSWORD");
                return resp;
            }

            resp.setUser_no(userInfo.getUser_no());
            resp.setResp_code(Constant.SUCCESS);
            resp.setResp_msg("SUCCESS");
        } catch (GcServiceException e) {
            resp.setResp_code(e.getErrorCode());
            resp.setResp_msg(e.getMessage());
        } catch (Exception e) {
            logger.error(req +"_" + e.getMessage());

            resp.setResp_code(Constant.USERBASE_SYSTERM_ERROR);
            resp.setResp_msg("USERBASE_SYSTERM_ERROR");
        }

        logger.info("返回  检验用户登录密码(checkUserLoginPasswd) 响应:" + resp);
        return resp;
    }

}
